package com.shujia.kafka

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import java.util.Properties

object Demo1KafkaProducer {
  def main(args: Array[String]): Unit = {

    val properties = new Properties()
    //kafka 集群地址
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")

    //key和value的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    //创建生产者，本质是创建和kafka的连接
    val producer = new KafkaProducer[String, String](properties)

    //构建一行数据
    //必选的参数是topic和value,  分区默认轮询，时间戳默认是系统时间。key默认是null
    val record = new ProducerRecord[String, String]("bigdata", "大数据")

    //生成数据到kafka中
    producer.send(record)

    //关闭连接
    producer.close()
  }
}
